//
//  DepartmentDao.m
//  Contacts
//
//  Created by 武 娇 on 14-7-28.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "DepartmentDao.h"
#import "ContactDao.h"
@implementation DepartmentDao

-(BOOL)insertDepartment:(Department *)department{
    BOOL success = YES;
	[db executeUpdate:[self SQL:@"INSERT INTO %@ (id,name,sort) VALUES(?,?,?)" inTable:Department_TABLE],
     @(department.ID),
     department.name,
    @(department.sort)];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}

-(BOOL)insertDepartments:(NSArray *)departments{
    BOOL success = YES;
    for (Department *department in departments) {
        [self insertDepartment:department];
        if ([db hadError]) {
            NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
            success = NO;
            break;
        }
    }
	return success;
}

-(BOOL)deleteDepartment:(Department *)department{
    BOOL success = NO;
    [db executeUpdate:[self SQL:@"delete from %@ where id =?" inTable:Department_TABLE],
     department.ID
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
    return success;
}

-(BOOL)upateDepartment:(Department *)department{
    BOOL success = YES;
    [db executeUpdate:[self SQL:@"UPDATE %@ SET name =?,sort = ? WHERE id = ?" inTable:Notice_TABLE],
     department.name,
     @(department.sort),
     @(department.ID)
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}

-(BOOL)upateDepartments:(NSArray *)departments{
    BOOL success = YES;
    for (Department *department in departments) {
        [self upateDepartment:department];
        if ([db hadError]) {
            NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
            success = NO;
            break;
        }
    }
	return success;
}

-(NSArray *)getDepartments{
    NSMutableArray *departments = nil;
    FMResultSet *rs =[db executeQuery:[self SQL:@"select * from %@ order by sort desc" inTable:Department_TABLE]];
    ContactDao *contactDao = [[ContactDao alloc]init];
    while ([rs next]) {
        if (!departments) {
            departments = [NSMutableArray arrayWithCapacity:10];
        }
        Department *department = [self analysisDepartmentWithRS:rs];
        department.contacts = [contactDao getContactsWithDepartmentID:department.ID];
       [departments addObject:department];
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return departments;
}

-(BOOL)clearDepartments{
    BOOL success = YES;
	[db executeUpdate:[self SQL:@"delete from %@" inTable:Department_TABLE]];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
        success = NO;
	}
    return success;
}

-(Department *)analysisDepartmentWithRS:(FMResultSet *)rs{
    Department *department = [[Department alloc]init];
    department.ID = [rs intForColumn:@"id"];
    department.name =[rs stringForColumn:@"name"];
    department.sort = [rs intForColumn:@"sort"];
    return department;
}
@end
